package com.feng.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.feng.eduservice.entity.EduSubject;
import com.feng.eduservice.entity.excel.SubjectData;
import com.feng.eduservice.service.EduSubjectService;
import com.feng.servicebase.excecption.GuliException;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public class SubjecetListener extends AnalysisEventListener<SubjectData> {

    private EduSubjectService eduSubjectService;

    public SubjecetListener(){

    }
    public SubjecetListener(EduSubjectService eduSubjectService){
         this.eduSubjectService=eduSubjectService;
    }

    @Override
    @Transactional
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        if(subjectData==null){
            throw new GuliException(20001,"文件数据为空");
        }
        //完成一级分类添加
        String pid=null;
        //一级分类为空
        if (existOneSubject(eduSubjectService, subjectData.getOneSubjectName())) {
            //为真，没有相同的分类，进行添加
            EduSubject eduSubject=new EduSubject();
            eduSubject.setParentId("0");
            eduSubject.setTitle(subjectData.getOneSubjectName());
            eduSubjectService.save(eduSubject);
            //保存到数据库后将生成的pid进行获取
            pid=eduSubject.getId();

        }
        //不为空,也需要得到pid，否则后面都获取不到pid
        LambdaQueryWrapper<EduSubject> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EduSubject::getTitle,subjectData.getOneSubjectName());
        EduSubject one = eduSubjectService.getOne(queryWrapper);
        pid=one.getId();
        //得到2级分类的id值,获取1级分类的id值

        if(existTwoSubject(eduSubjectService,subjectData.getTwoSubjectName(),pid)){
            EduSubject eduSubject=new EduSubject();
            eduSubject.setTitle(subjectData.getTwoSubjectName());
            eduSubject.setParentId(pid);
            eduSubjectService.save(eduSubject);
        }

    }
    //判断一级分类不能重复添加,如果为空，返回true
   private Boolean existOneSubject(EduSubjectService eduSubjectService,String name){
       LambdaQueryWrapper<EduSubject> queryWrapper=new LambdaQueryWrapper<>();
       queryWrapper.eq(EduSubject::getParentId,0);
       queryWrapper.eq(EduSubject::getTitle,name);
       List<EduSubject> list = eduSubjectService.list(queryWrapper);
       if(list.isEmpty()){
           return true;
       }
       else {
           return false;
       }


   }
    //判断二级分类不能重复添加,如果为空，返回true
    private Boolean existTwoSubject(EduSubjectService eduSubjectService,String name,String pid){
        LambdaQueryWrapper<EduSubject> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EduSubject::getParentId,pid);
        queryWrapper.eq(EduSubject::getTitle,name);
        List<EduSubject> list = eduSubjectService.list(queryWrapper);
        if(list.isEmpty()){
            return true;
        }
        else {
            return false;
        }


    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
